var ChargingMessage = {
    pcReady: false,
    sendMsgInterval: null,
    init: function () {
        if (isMacI4tools()) {
            $('.footer').css('background', 'rgba(239, 241, 244, 1)');
        }
        window['cr'] = {
            receiveMessage: function (e) {
                console.log(e);
                var msg = JSON.parse(e);
                if (msg.event == 406) {
                    zz_send(JSON.stringify({event: 404}));
                }
            }
        };
        // 兼容V8
        window._pcReady = function () {
            ChargingMessage.pcReady = true;
        };
        InitUserInfoByHref('', '', function (res) {
            $('#num').text(res.frequency > 9999 ? '9999+' : res.frequency);
            $('#num').attr('title', res.frequency);
            $('#price').text(res.checkPrice);
            $('.balance').show();
            $('.unLogin').hide();
            $('.unLoginBalance').hide();
            if (ChargingMessage.versionStringCompare(isMacI4tools() ? '1.15' : '7.98.60', localStorage.getItem('i4Version').toString()) == -1) {
                if (res.hasReport == 1 & localStorage.getItem('reCheck') == 0) {
                    $('.charge').hide();
                    $('.free').show();
                    if (res.lastReportDate) {
                        $('.lastReportDate').show();
                        $('.lastReportDate').text('此设备上次验机时间：' + res.lastReportDate);
                    }
                } else if (res.hasReport == 0 | localStorage.getItem('reCheck') == 1) {
                    $('.free').hide();
                    $('.charge').show();
                }
            } else {
                $('.charge').show();
            }
            ChargingMessage.sendMsgToPc();
        }, function () {
            console.log('未登录');
            $('.free').hide();
            $('.charge').hide();
            $('.balance').hide();
            $('.unLogin').show();
            $('.unLoginBalance').show();
            ChargingMessage.sendMsgToPc();
        });

        $('#pay').click(function () {
            window.location.href = 'purchase.html';
        });
        $('#toLoginPage').click(function () {
            window.location.href = 'login.html';
        });
        $('.footerCancel').on('click', function () {
            zz_send(JSON.stringify({event: 404}));
        });
        $('#showHistory').on('click', function () {
            zz_send(JSON.stringify({event: 403}));
        });
        $('#rePay').on('click', function () {
            zz_send(JSON.stringify({event: 407}));
        });
        $('#payReport').on('click', function () {
            if (localStorage.getItem('reCheck') == 1) {
                zz_send(JSON.stringify({event: 407}));
            } else {
                zz_send(JSON.stringify({event: 403}));
            }
        });
    },
    versionStringCompare: function (preVersion, lastVersion) {
        var sources = preVersion.split('.');
        var dests = lastVersion.split('.');
        var maxL = Math.max(sources.length, dests.length);
        var result = 0;
        for (var i = 0; i < maxL; i++) {
            var preValue = sources.length > i ? sources[i] : 0;
            var preNum = isNaN(Number(preValue)) ? preValue.charCodeAt() : Number(preValue);
            var lastValue = dests.length > i ? dests[i] : 0;
            var lastNum = isNaN(Number(lastValue)) ? lastValue.charCodeAt() : Number(lastValue);
            if (preNum < lastNum) {
                result = -1;
                break;
            } else if (preNum > lastNum) {
                result = 1;
                break;
            }
        }
        return result;
    },
    sendMsgToPc: function () {
        if (localStorage.getItem('elVersion') == 2) {
            ChargingMessage.sendMsgInterval = setInterval(function () {
                if (ChargingMessage.pcReady) {
                    zz_send(JSON.stringify({event: 100}));
                    zz_send(JSON.stringify({event: 405, title: '扣费提示'}));
                    clearInterval(ChargingMessage.sendMsgInterval);
                }
            }, 10);
            return;
        }
        try {
            zz_send(JSON.stringify({event: 100}));
            zz_send(JSON.stringify({event: 405, title: '扣费提示'}));
        } catch (e) {
        }
    }
};
